/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Drawing;

using SdlDotNet.Graphics;
using Sdl = SdlDotNet.Graphics;

namespace BlindShark.Widgets
{
	/// <summary>
	/// Description of Label.
	/// </summary>
	public class Label : IWidget
	{
		#region Static Elements
		static string _sDefaultFont = "";
		
		public static string DefaultFont
		{
			get {return _sDefaultFont;}
			set {_sDefaultFont = value;}
		}
		#endregion
		
		#region Variables
		string _sText;
		string _sFont;
		int _iFontSize;
		Color _clrColor;
		
		bool _bUnderline;
		bool _bItalic;
		bool _bBold;
		TextAlignment _taAlignment;
		
		Rectangle _rctLabel;
		Rectangle _rctTextArea;
		bool _bAutoHeight;
		bool _bAutoWidth;
		
		Surface _sfLabel;
		#endregion
		
		#region Accessors
		public string Text
		{
			get {return _sText;}
			set {_sText = value;}
		}
		
		public Color TextColor
		{
			get {return _clrColor;}
			set {_clrColor = value;}
		}
		
		public bool Underline
		{
			get {return _bUnderline;}
			set {_bUnderline = value;}
		}
		
		public bool Italic
		{
			get {return _bItalic;}
			set {_bItalic = value;}
		}
		
		public bool Bold
		{
			get {return _bBold;}
			set {_bBold = value;}
		}
		
		public int X
		{
			get {return _rctLabel.X;}
			set {_rctLabel.X = value;}
		}
		
		public int Y
		{
			get {return _rctLabel.Y;}
			set {_rctLabel.Y = value;}
		}
		
		public Point Location
		{
			get 
			{
				Point trueLocation = _rctLabel.Location;
				trueLocation.X += _rctTextArea.X;
				trueLocation.Y += _rctTextArea.Y;
				return trueLocation;
			}
			set {_rctLabel.Location = value;}
		}
		
		public Size Size
		{
			get {return _rctLabel.Size;}
			set {_rctLabel.Size= value;}
		}
		
		public int Width
		{
			get {return _rctLabel.Width;}
			set {_rctLabel.Width = value;}
		}
		
		public int Height
		{
			get {return _rctLabel.Height;}
			set {_rctLabel.Height = value;}
		}
		
		public int Bottom
		{
			get {return _rctLabel.Bottom;}
		}
		
		public int Right
		{
			get {return _rctLabel.Right;}
		}
		
		public Surface Surface
		{
			get {return _sfLabel;}
		}
		
		public bool AutoHeight
		{
			get {return _bAutoHeight;}
			set {_bAutoHeight = value;}
		}
		
		public bool AutoWidth
		{
			get {return _bAutoWidth;}
			set {_bAutoWidth = value;}
		}
		
		public TextAlignment TextAlignment
		{
			get {return _taAlignment;}
			set {_taAlignment = value;}
		}
		#endregion
		
		public Label(string text, int fontSize, Color color, TextAlignment alignment)
		{
			_sText = text;
			_sFont = _sDefaultFont;
			_iFontSize = fontSize;
			_clrColor = color;
			
			_bUnderline = false;
			_bItalic = false;
			_bBold = false;
			_taAlignment = alignment;
			
			_rctLabel = new Rectangle(0,0,1,1);
			_rctTextArea = new Rectangle(0,0,1,1);
		}
		
		#region IWidget Interface members
		public void HandleEvents()
		{
			
		}
		
		public void Init()
		{	
			Sdl.Font font;
			
			// Initializing the font
			font = new Sdl.Font(_sFont, _iFontSize);
			font.Underline = _bUnderline;
			font.Bold = _bBold;
			font.Italic = _bItalic;
			// Initializing the drawing area
			_rctTextArea.Size = font.SizeText(_sText);
			if (_bAutoHeight)
				_rctLabel.Height = _rctTextArea.Height;
			if (_bAutoWidth)
				_rctLabel.Width = _rctTextArea.Width;
			
			// Getting the Coordinates of the text depending on the alignment
			if ((_taAlignment == TextAlignment.TopLeft) || (_taAlignment == TextAlignment.TopMiddle) || (_taAlignment == TextAlignment.TopRight))
				_rctTextArea.Y = 0;
			else if ((_taAlignment == TextAlignment.CenterLeft) ||(_taAlignment == TextAlignment.CenterMiddle) || (_taAlignment == TextAlignment.CenterRight))
				_rctTextArea.Y = (_rctLabel.Height - _rctTextArea.Height) / 2;
			else
				_rctTextArea.Y = _rctLabel.Height - _rctTextArea.Height;
			
			if ((_taAlignment == TextAlignment.TopLeft) ||(_taAlignment == TextAlignment.CenterLeft) || (_taAlignment == TextAlignment.BottomLeft))
				_rctTextArea.X = 0;
			else if ((_taAlignment == TextAlignment.TopMiddle) ||(_taAlignment == TextAlignment.CenterMiddle) || (_taAlignment == TextAlignment.BottomMiddle))
				_rctTextArea.X = (_rctLabel.Width - _rctTextArea.Width) / 2;
			else
				_rctTextArea.X = _rctLabel.Width - _rctTextArea.Width;
			
			_sfLabel = font.Render(_sText, _clrColor);
		}
		
		public void CleanUp()
		{
			_sfLabel = null;
		}
		#endregion
	}
}
